import play.*;
import play.libs.*;

import java.util.*;

import com.avaje.ebean.*;

import logger.LogSchedular;
import models.*;

public class Global extends GlobalSettings {
    
    public void onStart(Application app) {
    	play.data.format.Formatters.register(Date.class, new DateFormatter());
    	LogSchedular.start();
        InitialData.insert(app);
        System.out.println("----current system time zone: " + TimeZone.getDefault().toString());
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        System.out.println("----set to: " + TimeZone.getDefault().toString());
    }
    
    public void onStop(Application app) {
    	LogSchedular.stop();
    }
    
    static class InitialData {
        
        public static void insert(Application app) {
            if(Ebean.find(Menu.class).findRowCount() == 0) {
                
                @SuppressWarnings("unchecked")
                Map<String,List<Object>> all = (Map<String,List<Object>>)Yaml.load("initial-data.yml");
               
                
                
//auto insert after this

			Ebean.save(all.get("accounts"));

			Ebean.save(all.get("roles"));

			Ebean.save(all.get("account_roles"));

			Ebean.save(all.get("menus"));

			Ebean.save(all.get("permissions"));


			Ebean.save(all.get("userlogs"));

			Ebean.save(all.get("vehicles"));

			Ebean.save(all.get("staffs"));

			Ebean.save(all.get("drivers"));

			Ebean.save(all.get("customers"));

			Ebean.save(all.get("insurances"));

			Ebean.save(all.get("repairrequests"));

			Ebean.save(all.get("repairs"));

			Ebean.save(all.get("part_purchases"));

			Ebean.save(all.get("part_consumes"));

			Ebean.save(all.get("safety_checks"));

			Ebean.save(all.get("violation_records"));

			Ebean.save(all.get("run_cards"));

			Ebean.save(all.get("driver_evaluations"));

			Ebean.save(all.get("driver_trainings"));

			Ebean.save(all.get("beforework_trains"));

			Ebean.save(all.get("check_works"));

			Ebean.save(all.get("accidents"));

			Ebean.save(all.get("bookings"));

			Ebean.save(all.get("dispatchs"));

			Ebean.save(all.get("dailysalarys"));

			Ebean.save(all.get("checkouts"));

			Ebean.save(all.get("income_moneys"));

			Ebean.save(all.get("bills"));

			Ebean.save(all.get("add_oil1s"));

			Ebean.save(all.get("add_oil2s"));

//auto insert before this
			
			//for performance test
			//Date today = new Date();
			//for (int i=0; i<10000; i++) {
				//Vehicle v = new Vehicle();
				//v.setPlate_number("abc" + i);
				//v.setBrand("brand" + i);
				//v.setBuy_date(today);
				//v.setCancle_date(today);
				//v.setEngine_number("000");
				//v.setExamine_date(today);
				//v.setFactory_date(today);
				//v.save();
				//}
            }
        }
        
    }
    
}